﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HodhodNews;
using HodhodNews.Storage;
using Googler.BLL;
using HodhodNews.Controllers;

namespace Googler.Web
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshNews();
        }
        List<News> NewsList { get; set; }

        private void RefreshNews()
        {
            //DateTime from = Convert.ToDateTime(txtFrom.Text);
            //DateTime to = Convert.ToDateTime(txtTo.Text);
            int keywordId = Convert.ToInt32(Request.QueryString["KeywordId"]);
            if (keywordId <= 0)
            {
                Guid userId = new UserController().GetUserIdOrDefault();
                NewsList = StorageProvider.Insance.GetDayNews(DateTime.Now,  userId);
            }
            else
            {
                NewsList = StorageProvider.Insance.GetDayNews(DateTime.Now, keywordId);
            }
            renderNewsList();
        }

        private void renderNewsList()
        {
            if (NewsList == null || NewsList.Count == 0)
            {
                lblNewsCount.Text = "0";
                return;
            }

            DataBindUI();
        }

        string DocumentText;
        const string html = "<HTML dir=rtl ><body>{0}</body></HTML>";
        public virtual void DataBindUI()
        {

            if (NewsList == null || NewsList.Count == 0)
            {
                this.DocumentText = "";
                return;
            }

            string content = "";

            foreach (News atom in NewsList)
            {
                content += atom.RenderSummeries();
            }
            this.DocumentText = string.Format(html, content);
            Label1.Text = this.DocumentText;
            if (Convert.ToString(Request.QueryString["Keyword"]) != null)
            {
                lblNewsCount.Text = Convert.ToString(Request.QueryString["Keyword"]) +" > ";
                lblNewsCount.Text += "عدد الأخبار " + NewsList.Count.ToString() + "<br/>";
            }
            else
            {
                lblNewsCount.Text = "عدد الأخبار " + NewsList.Count.ToString() + "<br/>";
            }
        }
    }
}